<?php

namespace App\Http\Controllers;

use App\Models\SchoolUser;
use App\Models\User;
use App\Services\RoleService;
use App\Services\UserService;
use Illuminate\Http\Request;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator as FacadesValidator;

class UserController extends BaseController
{
    use ValidatesRequests;

    public function login(Request $request){

        $passport = request()->input("passport");
        $password = request()->input("password");

        $validator = FacadesValidator::make($request->all(), [
            'passport' => 'bail|required|min:5|max:30',
            'password' => 'required|min:5|max:30',
        ]);

        if ($validator->fails()) {
            return resFail(null, "参数错误", ERROR_CODE);
        }

        $password = md5($password);
        $user = User::query()->where("passport", $passport)->where("password", $password)->first();
        if(!$user){
            return resFail(null, "用户名密码错误", -1);
        }


        // Registered
        return resSucc(Auth::login($user), "登录成功");
    }

    public function signup(Request $request){

        $validator = FacadesValidator::make($request->all(), [
            'passport' => 'bail|required|min:5|max:30',
            'password' => 'required|min:5|max:30',
        ]);

        if ($validator->fails()) {
            return resFail(null, "参数错误", ERROR_CODE);
        }

        $passport = request()->input("passport");
        $password = request()->input("password");
        $password2 = request()->input("password2");
        $nickName = request()->input("nickname");

        if($password != $password2){
            return resFail(null, "密码输入不一致", ERROR_CODE);
        }

        if(!$nickName || $nickName == ""){
            $nickName = $passport;
        }

        $user_id = UserService::checkPassport($passport);

        if($user_id > 0){
            return resFail(null, "用户已存在", ERROR_CODE);
        }

        $password = md5($password);

        $user_id = UserService::createUser($passport, $password, $passport);

        if($user_id == 0){
            return resFail(null, "注册失败", ERROR_CODE);
        }

        return resSucc($user_id, "注册成功");
    }

    public function info(Request $request){

        $user_id = $request->id ?? Auth::id();
        $user = User::query()->find($user_id);

        $school_user = SchoolUser::where([
            'school_id' => $request->school_id,
            'user_id' => $user_id
        ])->whereIn('campus_id', [$request->campus_id, 0])->first();

        if(!$school_user){
            return resFail(null, "用户不存在");
        }

        $user->identity = $school_user->identity;
        $user->school_user = $school_user;

        if($school_user->campus_id == 0 && $school_user->identity == 'school'){
            $user->roles = RoleService::getAllPowerCode();
        }else {
            $role_id = RoleService::getUserRoleId($user_id);
            if($role_id){
                $user->roles = RoleService::getRoleCode($role_id);
            }else{
                return resFail(null, "当前用户不在任何权限组");
            }
        }


        return resSucc($user, "用户详情");
    }

    public function signout(){
        return resSucc(Auth::logout(), "退出成功");
    }

    public function update(){

        $user_id = request()->input("id") ?? Auth::id();
        $nickname = request()->input("nickname");
        $password = request()->input("password");
        $password2 = request()->input("password2");

        $user = User::find($user_id);

        if(!$user){
            return resFail(null, "用户不存在", ERROR_CODE);
        }

        if(request()->has("nickname")){
            if($nickname == ""){
                return resFail(null, "昵称不能为空", ERROR_CODE);
            }
            $user->nickname = $nickname;
        }

        if(request()->has("password")){
            if($password && $password != $password2){
                return resFail(null, "两次密码输入不一致", ERROR_CODE);
            }
            $user->password = md5($password);
        }

        if(!$user->save()){
            return resFail(null, "更新失败", ERROR_CODE);
        }

        return resSucc($user, "更新成功");
    }

}


